package com.swinw.asumall.order.interceptor;

import com.swinw.common.vo.MemberVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Member;
import java.util.LinkedHashMap;

/**
 * 自定义拦截器
 *  如果用户没有登录就不能访问订单系统
 */
@Component
public class UserLoginInterceptor implements HandlerInterceptor {

    public static ThreadLocal<LinkedHashMap<String,Object>> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        LinkedHashMap<String,Object> userLogin = (LinkedHashMap<String,Object>) session.getAttribute("userLogin");
        if(userLogin != null){
            //用户已经登录
            threadLocal.set(userLogin);
            return true;
        }else{
            //用户未登录，重定向到登录系统
            response.sendRedirect("http://auth.swinw.com/login.html");
            return false;
        }
    }
}
